package carousel

import (
	"app/common/constant"
	"app/common/response"
	"app/internal/model"
	"app/pkg/log"
	"net/http"

	"github.com/gin-gonic/gin"
)

//	@Summary	轮播图列表
//	@Schemes
//	@Description	轮播图列表 /api/carousel/list
//	@Tags			微信小程序轮播图
//	@Accept			mpfd
//	@success		200	{object}	response.JSONResult{data=[]model.Carousel}	"desc"
//	@Router			/api/carousel/list [GET]
func List(c *gin.Context) {
	list := model.NewCarouselModel().List(true)
	c.JSON(http.StatusOK, response.JSONResult{Data: list})
}

//	@Summary	管理后台轮播图列表
//	@Schemes
//	@Description	管理后台轮播图列表 /manager/carousel/list
//	@Tags			微信小程序轮播图
//	@Accept			mpfd
//	@success		200	{object}	response.JSONResult{data=[]model.Carousel}	"desc"
//	@Router			/manager/carousel/list [GET]
func ManagerList(c *gin.Context) {
	list := model.NewCarouselModel().List(false)
	c.JSON(http.StatusOK, response.JSONResult{Data: list})
}

type reqAdd struct {
	ImageUrl string `json:"imageUrl"`
	Name     string `json:"name"`
	JumpUrl  string `json:"jumpUrl"`
	Type     int    `json:"type"` //跳转类型 0无 1外部 2内部
}

//	@Summary	轮播图新增
//	@Schemes
//	@Description	轮播图新增 /manager/carousel/add
//	@Tags			微信小程序轮播图
//	@Accept			json
//	@Param			payload	body		reqAdd				true	"body"
//	@success		200		{object}	constant.ErrInfo	"desc"
//	@Router			/manager/carousel/add [POST]
func Add(c *gin.Context) {
	req := reqAdd{}
	if err := c.ShouldBind(&req); err != nil {
		log.NewError(c.GetString("OperationID"), "BindJSON failed", "err:", err.Error(), req)
		c.JSON(http.StatusOK, constant.ParamsFailed)
		return
	}
	carousel := &model.Carousel{
		ImageUrl: req.ImageUrl,
		JumpUrl:  req.JumpUrl,
		Name:     req.Name,
		Type:     req.Type,
	}
	err := model.NewCarouselModel().Add(carousel)
	if err == nil {
		c.JSON(http.StatusOK, constant.OK)
		return
	}
	log.NewError(c.GetString("OperationID"), err)
	c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "发生错误"})
}

type ReqEdit struct {
	Id       int64  `json:"id" binding:"required"`
	ImageUrl string `json:"imageUrl"`
	Name     string `json:"name"`
	JumpUrl  string `json:"jumpUrl"`
	Type     int    `json:"type"`
}

//	@Summary	轮播图编辑
//	@Schemes
//	@Description	轮播图新增 /manager/carousel/edit
//	@Tags			微信小程序轮播图
//	@Accept			json
//	@Param			payload	body		ReqEdit				true	"body"
//	@success		200		{object}	constant.ErrInfo	"desc"
//	@Router			/manager/carousel/edit [POST]
func Edit(c *gin.Context) {
	req := ReqEdit{}
	if err := c.ShouldBind(&req); err != nil {
		log.NewError(c.GetString("OperationID"), "BindJSON failed", "err:", err.Error(), req)
		c.JSON(http.StatusOK, constant.ParamsFailed)
		return
	}
	err := model.NewCarouselModel().Edit(map[string]interface{}{"id": req.Id}, map[string]interface{}{
		"image_url": req.ImageUrl,
		"name":      req.Name,
		"jump_url":  req.JumpUrl,
		"type":      req.Type,
	})
	if err == nil {
		c.JSON(http.StatusOK, constant.OK)
		return
	}
	log.NewError(c.GetString("OperationID"), err)
	c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "发生错误"})
}

type ReqDelete struct {
	Id int64 `json:"id"`
}

//	@Summary	轮播图删除
//	@Schemes
//	@Description	轮播图删除 /manager/carousel/delete
//	@Tags			微信小程序轮播图
//	@Accept			json
//	@Param			payload	body		ReqDelete			true	"body"
//	@success		200		{object}	constant.ErrInfo	"desc"
//	@Router			/manager/carousel/delete [POST]
func Delete(c *gin.Context) {
	req := ReqDelete{}
	if err := c.ShouldBind(&req); err != nil {
		log.NewError(c.GetString("OperationID"), "BindJSON failed", "err:", err.Error(), req)
		c.JSON(http.StatusOK, constant.ParamsFailed)
		return
	}
	err := model.NewCarouselModel().Delete(req.Id)
	if err != nil {
		log.NewError(c.GetString("OperationID"), "Delete failed", "err:", err.Error())
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "发生错误"})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}
